package prv.pursuivant.render.html;

import java.util.Date;
import java.util.List;

import prv.pursuivant.core.AchievType;
import prv.pursuivant.core.AchievTypesTree;
import prv.pursuivant.core.AllToons;
import prv.pursuivant.core.Toon;
import prv.pursuivant.core.impl.KindImpl;
import prv.pursuivant.render.DataRenderer;
import prv.pursuivant.render.OutputGenerator;
import prv.pursuivant.render.TableRenderer;

import com.google.inject.Inject;
import com.google.inject.internal.Lists;

public class HtmlTableRenderer implements TableRenderer {

	private static final int ACHIEVS_PER_PAGE = 17;
	private final AchievTypesTree tree;
	private final AllToons toons;

	@Inject
	public HtmlTableRenderer(AchievTypesTree tree, AllToons toons) {
		this.tree = tree;
		this.toons = toons;
	}

	@Override
	public void renderTable(OutputGenerator generator, AchievType achiev,
			Date when, DataRenderer dataRenderer) {

		List<List<String>> pages = divideCrits(achiev.getCriteria());

		int i = 0;

		for (List<String> page : pages) {

			i++;

			List<AchievType> criteria = Lists.newArrayList();
			for (String critId : page)
				criteria.add(tree.findAchievTypeById(critId));

			generator.elem("table").attr("class", "achievements");
			generator.elem("caption");
			generator.text(achiev.getName());
			if (pages.size() > 1)
				generator.text(" (" + i + "/" + pages.size() + ")");
			generator.end();

			generator.emptyElem("colgroup").attr("span", "1").attr("class",
					"toons");
			generator.emptyElem("colgroup").attr("span", "" + criteria.size())
					.attr("class", "achievements");

			renderTHead(generator, criteria, when, dataRenderer);
			renderTBody(generator, criteria, when, dataRenderer);
			generator.end();
		}
	}

	public void renderTHead(OutputGenerator generator,
			List<AchievType> criteria, Date when, DataRenderer dr) {
		generator.elem("thead");
		generator.elem("tr");
		generator.elem("th").entRef("nbsp").end(); // topleft cell
		for (AchievType crit : criteria) {
			generator.elem("th");
			generator.attr("class", crit.getKind().toString().toLowerCase());
			dr.renderCriteriaText(generator, crit);
			generator.end();
		}
		generator.end();
		generator.end();
	}

	public void renderTBody(OutputGenerator generator,
			List<AchievType> criteria, Date when, DataRenderer dr) {
		generator.elem("tbody");
		for (Toon toon : toons) {
			generator.elem("tr").attr("onmouseover", "this.bgColor='#AF7817';")
					.attr("onmouseout", "this.bgColor='';"); // I really really
			// don't want it
			// here
			generator.elem("td").attr("class", "toon");
			dr.renderToonText(generator, toon);
			generator.end();
			for (AchievType crit : criteria) {
				dr.renderAchievCell(generator, toon, crit, when);
			}
			generator.end();
		}
		generator.end();
	}

	public List<List<String>> divideCrits(List<String> ids) {
		int total = ids.size();
		int perPage = ACHIEVS_PER_PAGE;
		if (perPage < total) {
			int pages = (total + perPage - 1) / perPage;
			perPage = (total + pages - 1) / pages;
		}

		List<List<String>> result = Lists.newArrayList();
		for (String id : ids) {
			List<String> page;
			if (result.isEmpty()
					|| result.get(result.size() - 1).size() == perPage) {
				page = Lists.newArrayList();
				result.add(page);
			}
			page = result.get(result.size() - 1);
			page.add(id);
		}
		return result;
	}
}
